#!/usr/bin/env python
# coding: utf-8
'''
    建立一个分享的站点
    by: zhuxuezheng [at] gmail.com
'''
__revision__ = '0.1'

import os
import web
import user.userapp
import index.indexapp
import t.tapp
from SakyaConfig import get

from sqlalchemy.orm import scoped_session
import models_ds

urls = ('/index.htm', index.indexapp.app,
        '/user', user.userapp.app,
        '/t', t.tapp.app,
        '/', index.indexapp.app)

web.config.debug = False
#web.py自带的模版(不使用) render = web.template.render('/xx/templates/')

def load_orm(handler):
    ''' 装载DB-ORM '''
    web.ctx.orm = scoped_session(models_ds.sessionFac)
    try:
        return handler()
    finally:
        pass
        #web.ctx.orm.commit()

app = web.application(urls, globals(), True)

session = web.session.Session(app, web.session.DiskStore(get('session', 'dir')), initializer = {})
def session_hook():
    ''' 增加Session支持 '''
    web.ctx.session = session

app.add_processor(web.loadhook(session_hook))
app.add_processor(load_orm)

if __name__ == "__main__":
    ''' 运行应用 '''
    app.run()
